<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('imrStudent.fields.accountId')" prop="accountId">
        <el-input
          v-model="formData.accountId"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.accountId')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.studentPhoto')" prop="studentPhoto">
        <el-input
          v-model="formData.studentPhoto"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.studentPhoto')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.studentCode')" prop="studentCode">
        <el-input
          v-model="formData.studentCode"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.studentCode')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.studentName')" prop="studentName">
        <el-input
          v-model="formData.studentName"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.studentName')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.grade')" prop="grade">
        <el-input
          v-model="formData.grade"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.grade')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.classroom')" prop="classroom">
        <el-input
          v-model="formData.classroom"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.classroom')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.studentType')" prop="studentType">
        <el-select
          v-model="formData.studentType"
          :placeholder="t('common.selectTextPlaceholder') + t('imrStudent.fields.studentType')"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.inschoolFlag')" prop="inschoolFlag">
        <el-input
          v-model="formData.inschoolFlag"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.inschoolFlag')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.identityNumber')" prop="identityNumber">
        <el-input
          v-model="formData.identityNumber"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.identityNumber')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.trainingLevel')" prop="trainingLevel">
        <el-input
          v-model="formData.trainingLevel"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.trainingLevel')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.primaryMentorId')" prop="primaryMentorId">
        <el-input
          v-model="formData.primaryMentorId"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.primaryMentorId')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.primaryMentorCode')" prop="primaryMentorCode">
        <el-input
          v-model="formData.primaryMentorCode"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.primaryMentorCode')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.primaryMentor')" prop="primaryMentor">
        <el-input
          v-model="formData.primaryMentor"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.primaryMentor')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.researchDeptId')" prop="researchDeptId">
        <el-input
          v-model="formData.researchDeptId"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.researchDeptId')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.researchDept')" prop="researchDept">
        <el-input
          v-model="formData.researchDept"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.researchDept')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.originalCollege')" prop="originalCollege">
        <el-input
          v-model="formData.originalCollege"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.originalCollege')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.gender')" prop="gender">
        <el-input
          v-model="formData.gender"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.gender')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.birthdate')" prop="birthdate">
        <el-date-picker
          v-model="formData.birthdate"
          type="date"
          value-format="x"
          :placeholder="t('common.selectTextPlaceholder') + t('imrStudent.fields.birthdate')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.ethnic')" prop="ethnic">
        <el-input
          v-model="formData.ethnic"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.ethnic')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.politicalStatus')" prop="politicalStatus">
        <el-radio-group v-model="formData.politicalStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.homeAddress')" prop="homeAddress">
        <el-input
          v-model="formData.homeAddress"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.homeAddress')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.homeTelephone')" prop="homeTelephone">
        <el-input
          v-model="formData.homeTelephone"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.homeTelephone')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.personalPhone1')" prop="personalPhone1">
        <el-input
          v-model="formData.personalPhone1"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.personalPhone1')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.personalEmail1')" prop="personalEmail1">
        <el-input
          v-model="formData.personalEmail1"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.personalEmail1')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.arpNumber')" prop="arpNumber">
        <el-input
          v-model="formData.arpNumber"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.arpNumber')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudent.fields.remark')" prop="remark">
        <el-input
          v-model="formData.remark"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudent.fields.remark')"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ImrStudentApi, ImrStudentVO } from '@/api/core/imrstudent'
import { FormRules } from 'element-plus'

/** 联培学生信息 表单 */
defineOptions({ name: 'ImrStudentForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  accountId: undefined,
  studentPhoto: undefined,
  studentCode: undefined,
  studentName: undefined,
  grade: undefined,
  classroom: undefined,
  studentType: undefined,
  inschoolFlag: undefined,
  identityNumber: undefined,
  trainingLevel: undefined,
  primaryMentorId: undefined,
  primaryMentorCode: undefined,
  primaryMentor: undefined,
  researchDeptId: undefined,
  researchDept: undefined,
  originalCollege: undefined,
  gender: undefined,
  birthdate: undefined,
  ethnic: undefined,
  politicalStatus: undefined,
  homeAddress: undefined,
  homeTelephone: undefined,
  personalPhone1: undefined,
  personalEmail1: undefined,
  arpNumber: undefined,
  remark: undefined
})
const formRules = reactive<FormRules>({
  id: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.id'),
      trigger: 'blur'
    }
  ],
  accountId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.accountId'),
      trigger: 'blur'
    }
  ],
  studentPhoto: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.studentPhoto'),
      trigger: 'blur'
    }
  ],
  studentCode: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.studentCode'),
      trigger: 'blur'
    }
  ],
  studentName: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.studentName'),
      trigger: 'blur'
    }
  ],
  grade: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.grade'),
      trigger: 'blur'
    }
  ],
  classroom: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.classroom'),
      trigger: 'blur'
    }
  ],
  studentType: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.studentType'),
      trigger: 'change'
    }
  ],
  inschoolFlag: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.inschoolFlag'),
      trigger: 'blur'
    }
  ],
  identityNumber: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.identityNumber'),
      trigger: 'blur'
    }
  ],
  trainingLevel: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.trainingLevel'),
      trigger: 'blur'
    }
  ],
  primaryMentorId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.primaryMentorId'),
      trigger: 'blur'
    }
  ],
  primaryMentorCode: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.primaryMentorCode'),
      trigger: 'blur'
    }
  ],
  primaryMentor: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.primaryMentor'),
      trigger: 'blur'
    }
  ],
  researchDeptId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.researchDeptId'),
      trigger: 'blur'
    }
  ],
  researchDept: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.researchDept'),
      trigger: 'blur'
    }
  ],
  originalCollege: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.originalCollege'),
      trigger: 'blur'
    }
  ],
  gender: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.gender'),
      trigger: 'blur'
    }
  ],
  birthdate: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.birthdate'),
      trigger: 'blur'
    }
  ],
  ethnic: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.ethnic'),
      trigger: 'blur'
    }
  ],
  politicalStatus: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.politicalStatus'),
      trigger: 'blur'
    }
  ],
  homeAddress: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.homeAddress'),
      trigger: 'blur'
    }
  ],
  homeTelephone: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.homeTelephone'),
      trigger: 'blur'
    }
  ],
  personalPhone1: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.personalPhone1'),
      trigger: 'blur'
    }
  ],
  personalEmail1: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.personalEmail1'),
      trigger: 'blur'
    }
  ],
  arpNumber: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.arpNumber'),
      trigger: 'blur'
    }
  ],
  remark: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.remark'),
      trigger: 'blur'
    }
  ],
  creator: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.creator'),
      trigger: 'blur'
    }
  ],
  createTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.createTime'),
      trigger: 'blur'
    }
  ],
  updater: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.updater'),
      trigger: 'blur'
    }
  ],
  updateTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.updateTime'),
      trigger: 'blur'
    }
  ],
  deleted: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.deleted'),
      trigger: 'blur'
    }
  ],
  tenantId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudent.fields.tenantId'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ImrStudentApi.getImrStudent(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ImrStudentVO
    if (formType.value === 'create') {
      await ImrStudentApi.createImrStudent(data)
      message.success(t('common.createSuccess'))
    } else {
      await ImrStudentApi.updateImrStudent(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    accountId: undefined,
    studentPhoto: undefined,
    studentCode: undefined,
    studentName: undefined,
    grade: undefined,
    classroom: undefined,
    studentType: undefined,
    inschoolFlag: undefined,
    identityNumber: undefined,
    trainingLevel: undefined,
    primaryMentorId: undefined,
    primaryMentorCode: undefined,
    primaryMentor: undefined,
    researchDeptId: undefined,
    researchDept: undefined,
    originalCollege: undefined,
    gender: undefined,
    birthdate: undefined,
    ethnic: undefined,
    politicalStatus: undefined,
    homeAddress: undefined,
    homeTelephone: undefined,
    personalPhone1: undefined,
    personalEmail1: undefined,
    arpNumber: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
